Page({
  data: {
    historyList: [],
    loading: false,
    finished: false
  },

  onLoad() {
    this.loadHistory();
  },

  // 加载浏览历史
  async loadHistory() {
    if (this.data.loading || this.data.finished) return;
    
    this.setData({ loading: true });
    
    try {
      // TODO: 调用获取浏览历史接口
      // const res = await getHistory();
      const res = {
        data: []
      };
      
      this.setData({
        historyList: [...this.data.historyList, ...res.data],
        finished: res.data.length < 10
      });
    } catch (error) {
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 清空浏览历史
  clearHistory() {
    wx.showModal({
      title: '提示',
      content: '确定要清空浏览历史吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            // TODO: 调用清空浏览历史接口
            // await clearHistory();
            wx.showToast({
              title: '已清空',
              icon: 'success'
            });
            this.setData({ historyList: [] });
          } catch (error) {
            wx.showToast({
              title: '操作失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  // 跳转到商品详情
  goToDetail(event) {
    const { id } = event.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/product/detail/index?id=${id}`
    });
  }
}); 